//
//  DoctorClassroom.swift
//  FiveProvincesOfNorthChina
//
//  Created by kys-20 on 2019/9/13.
//  Copyright © 2019 kys-20. All rights reserved.
//

import UIKit
import MobileCoreServices
import AssetsLibrary
import AVKit
import AVFoundation
import MediaPlayer
class DoctorClassroomController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate,doctorClassroomProtocol{

    var vie : DoctorClassroom!
    var urlArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = blueColor
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (UIScreen.main.bounds.width-45)/2, height: (UIScreen.main.bounds.width-45)/2*(3/5)+40)
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.sectionInset = UIEdgeInsets(top: 15,left: 15,bottom: 15,right: 15)
        let fram = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        vie = DoctorClassroom(frame: fram, collectionViewLayout: layout)
        vie.delegateClass = self
        self.view.addSubview(vie)
        
        getData()
         self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]//设置导航栏上标题颜色
        
    }
    
    func getData(){
        DoctorClassModel.getVideoData { (modelArray) in
             self.urlArray = [String]()
             var nameArray = [String]()
            for i in 0...modelArray!.count-1{
                self.urlArray.append(modelArray![i].url)
                nameArray.append(modelArray![i].name)
            }
             self.vie.setUpData(imageArray: self.urlArray, labelArray: nameArray)
            
        }
    }
    

    @objc func upButtonAction(){
        print("上传")
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            //初始化图片控制器
            let imagePicker = UIImagePickerController()
            //设置代理
            imagePicker.delegate = self
            //指定图片控制器类型
            imagePicker.sourceType = .photoLibrary
            //只显示视频类型的文件
            imagePicker.mediaTypes = [kUTTypeMovie as String]
            //不需要编辑
            imagePicker.allowsEditing = false
            //弹出控制器，显示界面
            self.present(imagePicker, animated: true, completion: nil)
        }else{
            print("读取相册错误")
        }
        
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        //获取视频路径（选择后视频会自动复制到app临时文件夹下）
        let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! URL
        let pathString = videoURL.relativePath
        print("视频地址：\(pathString)")
        //图片控制器退出
        self.dismiss(animated: true, completion: {})
        //播放视频文件
//        reviewVideo(videoURL)
    }
    //视频播放
    func celldidSelect(int: Int) {

        let videoURL =  URL(string: "\(domainName)\(self.urlArray[int])")!
        //定义一个视频播放器，通过本地文件路径初始化
        let player = AVPlayer(url: videoURL)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }
    }
}
